//
//  UIApplication+LCExtension.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "UIApplication+LCExtension.h"

@implementation UIApplication (LCExtension)

+ (void)LC_openURL_phone:(NSString *)phone {
    NSString *tel = [NSString stringWithFormat:@"tel:%@", phone];
    NSURL *url = [NSURL URLWithString:tel];
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}

//获取当前屏幕显示的viewcontroller
+ (UIViewController *)getCurrentVC {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    
    UIViewController *currentVC = window.rootViewController;
    
    BOOL runLoopFind = YES;
    
    while (runLoopFind) {
        if (currentVC.presentedViewController) {
            currentVC = currentVC.presentedViewController;
        } else if ([currentVC isKindOfClass:[UINavigationController class]]) {
            UINavigationController *naVC = (UINavigationController *)currentVC;
            currentVC = [naVC.childViewControllers lastObject];
        } else if ([currentVC isKindOfClass:[UITabBarController class]]) {
            UITabBarController *tabBarVC = (UITabBarController *)currentVC;
            currentVC = tabBarVC.selectedViewController;
        } else {
            NSUInteger childVCCount = currentVC.childViewControllers.count;
            if (childVCCount > 0) {
                currentVC = currentVC.childViewControllers.lastObject;
                return currentVC;
            } else {
                return currentVC;
            }
        }
    }
    return currentVC;
}

@end
